const dav = require('dav')

const card = `BEGIN:VCARD
VERSION:3.0
FN:Forrest Gump
ORG:Bubba Gump Shrimp Co.
EMAIL;TYPE=PREF,INTERNET:forrestgump@example.com
TITLE:Soldier\\,Player
REV:2008-04-24T19:52:43Z
END:VCARD`


let carddav_test = async ()=>{
    let addressBooks, xhr;

    xhr = new dav.transport.Basic(
        new dav.Credentials({
            username: 'admin',
            password: 'root'
        })
    );

    let account = await dav.createAccount({
        server: 'http://localhost:18081/remote.php/dav/addressbooks/users/admin/contacts',
        xhr: xhr,
        accountType: 'carddav',
        loadObjects: true
    });

    let addressBook = account.addressBooks[0];
    let objects = addressBook.objects;

    await dav.createCard(addressBook, {
        filename: 'Gump_1.vcf',
        data: card,
        xhr: xhr
    });

    let updated = await dav.syncCarddavAccount(account, {
        syncMethod: 'basic',
        xhr: xhr
    });

    addressBooks = updated.addressBooks;

    addressBook = addressBooks[0];
    objects = addressBook.objects;

}





if (require.main === module) {
    carddav_test().then(() => {
            console.log('data migrate finished');
            process.exit(0);
        }
    ).catch((err) => {
        console.log(err)
    })
}
